<?php
declare(strict_types=1);


namespace App\Fastapp\Mapper;

use App\Fastapp\Model\FastappChannelAgentSettlement;
use Hyperf\Database\Model\Builder;
use Mine\Abstracts\AbstractMapper;

/**
 * 代理结算Mapper类
 */
class FastappChannelAgentSettlementMapper extends AbstractMapper
{
    /**
     * @var FastappChannelAgentSettlement
     */
    public $model;

    public function assignModel()
    {
        $this->model = FastappChannelAgentSettlement::class;
    }

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        // 用户id
        if (isset($params['user_id']) && filled($params['user_id'])) {
            $query->where('user_id', '=', $params['user_id']);
        }

        // 发放0否1是
        if (isset($params['status']) && filled($params['status'])) {
            $query->where('status', '=', $params['status']);
        }


        // 
        if (isset($params['start_at']) && filled($params['start_at']) && is_array($params['start_at']) && count($params['start_at']) == 2) {
            $query->whereBetween(
                'start_at',
                [$params['start_at'][0], $params['start_at'][1]]
            );
        }

        // 
        if (isset($params['end_at']) && filled($params['end_at']) && is_array($params['end_at']) && count($params['end_at']) == 2) {
            $query->whereBetween(
                'end_at',
                [$params['end_at'][0], $params['end_at'][1]]
            );
        }
        return $query->orderByDesc('id');
    }
}